/* @(#)GSBalanceCategoryDAO.java    V0.1
 *
 * Project: NEWGS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Simonyi Peng 2006-12-1  新增
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.dao;

import java.util.ArrayList;

import com.hitrust.gs.bean.GSBalanceCategoryBean;
import com.hitrust.gs.form.GSBalanceCategoryForm;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.db.GSBaseDAO;
import com.hitrust.gs.util.exception.GSException;
import java.sql.*;

/**
 * 
 */
public class GSBalanceCategoryDAO extends GSBaseDAO{
    /**
     * 查询所有公告表
     * @return
     * @throws GSException
     */
    public ArrayList queryAllBalanceCategory() throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "select * from GS_BALANCE_CATEGORY where GS_DIRTY = 1";
        try{
            tList = this.executeQuery(new GSBalanceCategoryBean(),tSql,null);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    /**
     * 查询公告
     * @return
     * @throws GSException
     */
    public GSBalanceCategoryBean queryBalanceCategory(String balanceCategoryNo) throws GSException{
    	GSBalanceCategoryBean tBean = null;
    	ArrayList tList = null;
        String tSql = "select * from GS_BALANCE_CATEGORY where GS_BALANCE_CATEGORY_NO = ?";
        Object[] obj = new String[]{balanceCategoryNo};
        try{
            tList = this.executeQuery(new GSBalanceCategoryBean(),tSql,obj);
            if(tList != null){
            	tBean = (GSBalanceCategoryBean)tList.get(0);
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tBean;
    }
    
    /**
     * 在结算分类表GS_BALANCE_CATEGORY中新增一笔记录
     * 
     * @param aGSBalanceFeeBean
     * 
     * @return boolean
     * 
     * @throws CUPSException
     */
    public boolean add(GSBalanceCategoryBean aGSBalanceCategoryBean) throws GSException {

        String tSql="INSERT INTO GS_BALANCE_CATEGORY (GS_BALANCE_CATEGORY_NO,GS_BALANCE_CATEGORY_NAME,GS_DESC,GS_DIRTY) VALUES(?,?,?,?)";

        Object[] tObject = new Object[4] ;
        
          tObject[0]=aGSBalanceCategoryBean.getGs_balance_category_no();
          tObject[1]=aGSBalanceCategoryBean.getGs_balance_category_name();
          tObject[2]=aGSBalanceCategoryBean.getGs_desc();
          tObject[3]=aGSBalanceCategoryBean.getGs_dirty();

          
        boolean tResult = false;
        try {
            tResult = executeUpdate(tSql, tObject);

        } catch (Exception ex) {
            GSLogWriter.error(this, ex.toString());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 在结算分类表GS_BALANCE_CATEGORY中更新一笔记录
     * 
     * @param aGSBalanceFeeBean
     * @throws CUPSException]
     * @return boolean
     */
    public boolean modify(GSBalanceCategoryBean aGSBalanceCategoryBean) throws GSException {

        String tSql = "UPDATE GS_BALANCE_CATEGORY SET GS_BALANCE_CATEGORY_NAME=?,GS_DESC=? WHERE GS_BALANCE_CATEGORY_NO = ?";
        Object[] tObject = new Object[3];
        tObject[0] = aGSBalanceCategoryBean.getGs_balance_category_name();
        tObject[1] = aGSBalanceCategoryBean.getGs_desc();
        tObject[2] = aGSBalanceCategoryBean.getGs_balance_category_no();

        boolean tResult = false;
        try {
            tResult = executeUpdate(tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 在结算分类表GS_BALANCE_CATEGORY中删除一笔记录
     * 
     * @param aTypeNo
     *            序列号
     */
    public boolean delete(String aBalanceCategoryNo) throws GSException {
        String tSql = "UPDATE GS_BALANCE_CATEGORY SET GS_DIRTY = 0 where GS_BALANCE_CATEGORY_NO=?";
        Object[] tObject = { aBalanceCategoryNo };
        boolean tResult = false;
        try {
            tResult=executeUpdate(tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    
    /**
     * 按照某个条件查询所有结算分类
     * @return
     * @throws GSException
     */
    public ArrayList queryOtherCategory(GSBalanceCategoryForm aGSBalanceCategoryForm) throws GSException{
        ArrayList tList = new ArrayList();
       
        StringBuffer tSql = new StringBuffer("select * from GS_BALANCE_CATEGORY where ");
        tSql.append("GS_BALANCE_CATEGORY_NAME like '%" + ("".equals(aGSBalanceCategoryForm.getBalanceCategoryName())?"":aGSBalanceCategoryForm.getBalanceCategoryName()) + "%'");
        if (!"".equals(aGSBalanceCategoryForm.getDesc()))
        	tSql.append(" and GS_DESC like '%" + aGSBalanceCategoryForm.getDesc() + "%'");
        tSql.append(" and GS_DIRTY = 1 ");
        tSql.append(" order by GS_BALANCE_CATEGORY_NO desc");
        try{
            tList = this.executeQuery(new GSBalanceCategoryBean(),tSql.toString(),null);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    public String queryMaxID() throws GSException {
    	String value  = "";
        String tSql = "select max(GS_BALANCE_CATEGORY_NO) from GS_BALANCE_CATEGORY ";
        Connection conn = null;
        try{
            conn = this.getConnetion();
            ResultSet rs = conn.prepareStatement(tSql).executeQuery();
            if(rs != null){
            	rs.next();
            	value = rs.getString(1); 
            	rs.close();
            }else{
            	value = "0";
            }
            
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        finally{
        	close(conn);
        }
        return value;
    	
    }
}
